import { defineStore } from "pinia";
import { useUserStore } from "./user";
import { ref } from "vue";


export const useGlobalStore = (key) => defineStore(`cp-global-${key}`,()=>{
    //全局變量:登錄用戶
    //空對象的變量 通過 as 斷言指定類型
    const globalActions = ref(null)

    //方法
    //存儲
    const setGlobalActions = (actions)=>{
        globalActions.value = actions
    }

    //同步
    const syncToMainApp = () => {
        console.log(globalActions.value)
        if(globalActions.value)
        {
            const store = useUserStore(key)
            globalActions.value.setGlobalState({
                useUserStore: store.user
            })
        }
    }

    //删除
    const delGlobalActions = ()=>{
        globalActions.value = null
    }

    return { globalActions, setGlobalActions, delGlobalActions, syncToMainApp }
},
// 方式1：默認存儲所有數據，以store的ID作為存儲的key
// {
//     persist:true  //開啟數據持久化
// },
//方式2：自定義存儲的key，指定存儲那些數據
{
    persist: {
        key: `he-cp-global-${key}`,
    }
})()